//
//  HLEArrayList.m
//  mj-01
//
//  Created by 宋宏康 on 2021/7/11.
//

#import "HLEArrayList.h"


#define RARRAY_LIST_DEFAULT_CAPACITY (1<<4)

@interface HLEArrayList()
@property(nullable, nonatomic, strong) NSMutableArray *elements;
@end

@implementation HLEArrayList
+ (instancetype)arrayList:(NSInteger)capaticy
{
    return [[self alloc] initWithCapacity:capaticy];
}

- (instancetype)initWithCapacity:(NSUInteger)capacity
{
    self = [super init];
    NSUInteger numItems = capacity < RARRAY_LIST_DEFAULT_CAPACITY ?  RARRAY_LIST_DEFAULT_CAPACITY : capacity;
    self.elements = [NSMutableArray arrayWithCapacity:numItems];
    return self;
}

- (void)add:(id)element
{
    self.elements[_size] = element;
    self.size++;
}

- (void)remove:(NSInteger)index
{
    for (NSInteger i = index + 1; i < _size; i++) {
        self.elements[i - 1] = self.elements[i];
    }
    self.elements[-- _size] = @-1;
}


- (NSString *)description
{
    NSMutableString *string = [NSMutableString string];
    [string appendString:[NSString stringWithFormat:@"[%@, %p]: size=%zd \n{ \n", [self class], self, _size]];
    [self.elements enumerateObjectsUsingBlock:^(id  _Nullable obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx) {
            [string appendString:@",\n"];
        }
        [string appendString:[NSString stringWithFormat:@"   %@", obj]];
        if (idx == _size - 1) {
            *stop = YES;
        }
    }];
    [string appendString:@"\n}"];
    return string;
}


@end
